/*
 * @Author: linZeJian
 * @Date: 2022-04-27 17:35:20
 * @LastEditors: linZeJian
 * @LastEditTime: 2022-10-09 10:39:48
 * @Description: 
 */

//时间戳转换方法 年-月-日   dateValue:时间戳数字
export function formatDate(dateValue, type) {
  if (!dateValue) {
    return ""
  }
  const date = new Date(dateValue);
  const YY = date.getFullYear();
  const MM = '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  const DD = '-' + (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
  const mm = ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  const ss = ':' + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  let result = ""
  switch (type) {
    case 6:
      result = YY + MM + DD + " " + hh + mm + ss;
      break;
    case 5:
      result = YY + MM + DD + " " + hh + mm;
      break;
    case 4:
      result = YY + MM + DD + " " + hh + ':00';
      break;
    case 3:
      result = YY + MM + DD;
      break;
    case 2:
      result = YY + MM;
      break;
    case 1:
      result = hh + mm + ss;
      break;
    default:
      result = YY + MM + DD + " " + hh + mm + ss;
  }
  return result
}

//vant-popup在有滚动条的页面，弹出遮罩层时页面会滚动到顶部,解决
export function getScrollTop() {
  // let scrollTop = 0;
  // if (document.documentElement && document.documentElement.scrollTop) {
  //     scrollTop = document.documentElement.scrollTop;
  // } else if (document.body) {
  //     scrollTop = document.body.scrollTop;
  // }
  let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  return scrollTop;
}

/**
 * @description: 获取当前域名，如http://www.baidu.com
 * @param {*}
 * @return {*}
 */
export function getCurrentDomain() {
  return window.location.protocol + '//' + window.location.host
}

/**
 * @description: a标签打开url
 * @param {*} url 地址
 * @param {*} isOpenNew 是否打开新页面
 * @return {*}
 */
export function openUrl(url, isOpenNew) {
  let a = document.createElement('a')
  a.setAttribute('href', url)
  if (isOpenNew) {
    a.setAttribute('target', '_blank')
  }
  a.setAttribute('id', 'd2admin-link-temp')
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}

/**
 * @description: 校验空值、null、undefined、[]
 * @param {*} val
 * @return {*}
 */
export function verification(val) {
  let valString = val + ""
  let verificationType = ["", "null", "undefined"]
  return verificationType.includes(valString)
}

/**
 * @description: 检测js的所有数据类型
 * @param {*} data
 * @return {*}
 */
export function dataTypes(data) {
  // Object.prototype.toString.call(111)
  // '[object Number]'
  // Object.prototype.toString.call(111).slice(8, -1).toLowerCase();
  // 'number'
  let dataType = Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
  return dataType
}

/**
 * @description: 获取滚动条位置
 * @param {*}
 * @return {*}
 */
export function getScroll() {
  var t, l, w, h;
  if (document.documentElement && document.documentElement.scrollTop) {
    t = document.documentElement.scrollTop;
    l = document.documentElement.scrollLeft;
    w = document.documentElement.scrollWidth;
    h = document.documentElement.scrollHeight;
  } else if (document.body) {
    t = document.body.scrollTop;
    l = document.body.scrollLeft;
    w = document.body.scrollWidth;
    h = document.body.scrollHeight;
  }
  return {
    t: t,
    l: l,
    w: w,
    h: h
  };
}

/**
 * @description: 初始化屏幕最多宽度，传参绑定class名
 * @param {*} className
 * @return {*}
 */
export function setClientMaxWidth(className) {
  return
  // console.log("document.body.clientWidth :>> ", document.body.clientWidth);
  // js动态获取宽度,设置样式
  let clientWidth = document.body.clientWidth;
  let fixedMarginLeft = (clientWidth - 1280) / 2;
  if (clientWidth > 1280) {
    // let fixedClass = document.getElementsByClassName(className)[0];
    //一次添加多个
    let fixedClassList = document.getElementsByClassName(className);
    let i;
    for (i = 0; i < fixedClassList.length; i++) {
      fixedClassList[i].setAttribute("style", "max-width: 1280px;");
      //style.样式名=样式值;
      fixedClassList[i].style.marginLeft = fixedMarginLeft + "px";
    }
  }
}

/**
 * @description: 判断是否为手机登录，是返回true
 * @param {*}
 * @return {*}
 */
export function isMobile() {
  // return true
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (
    bIsIpad ||
    bIsIphoneOs ||
    bIsMidp ||
    bIsUc7 ||
    bIsUc ||
    bIsAndroid ||
    bIsCE ||
    bIsWM
  ) {
    console.log("手机登录 return true");
    return true
  } else {
    console.log("电脑登录 return false");
    return false
  }

}

/**
 * @description: 滚动节流
 * @param {*} func
 * @param {*} delay
 * @return {*}
 */
export function throttle(func, delay) {
  var timer = null;
  return function () {
    var context = this;
    var args = arguments;
    if (!timer) {
      timer = setTimeout(function () {
        func.apply(context, args);
        timer = null;
      }, delay);
    }
  }
}